package com.neiru.pruebas.ar.view;

import java.io.IOException;
import java.util.List;

import android.content.Context;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class CustomCameraView extends SurfaceView {

	Camera camera;
	
	SurfaceHolder previewHolder;
	
	SurfaceHolder.Callback surfaceHolderListener = new SurfaceHolder.Callback() {
		
		public void surfaceDestroyed(SurfaceHolder holder) {
			camera.stopPreview();
			camera.release();
		}
		
		public void surfaceCreated(SurfaceHolder holder) {
			camera = Camera.open();
			try {
				camera.setPreviewDisplay(previewHolder);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		
		public void surfaceChanged(SurfaceHolder holder, int format, int width,
				int height) {
			Parameters params = camera.getParameters();
			List<Camera.Size> previewSizes = params.getSupportedPreviewSizes();
			Camera.Size previewSize = previewSizes.get(0);
			params.setPreviewSize(previewSize.width, previewSize.height);
			params.setPictureFormat(PixelFormat.JPEG);
			camera.setParameters(params);
			camera.startPreview();
		}
	};
	
	public CustomCameraView(Context context) {
		super(context);
		previewHolder = this.getHolder();
		previewHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
		previewHolder.addCallback(surfaceHolderListener);
	}

}
